/**
 * @file 工单操作-非标信息
 * @author songweidong
 * @date 2020-07-31 16:20:25
 */
layui.define(['layer', 'upload', 'form', 'table', 'list_modify_common', '$http'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let opCommonUtils = layui.list_modify_common;
    let $http = layui.$http;
    let layer = layui.layer;
    let upload = layui.upload;
    let table = layui.table;
    let timestamp = new Date().getTime();

    let cbList = [];

    function renderDetailList(data) {
        return table.render({//使用配件清单
            elem: '#nonstandard',
            height: 200,
            skin: 'line',
            cols: [
                [
                    {field: 'nonstandardId', title: 'id', hide: true},
                    {field: 'title', title: '信息名称', fixed: 'left', minWidth: 25 , edit:'text',templet:function(d){
                        if(d.title.length <= 20){
                            return d.title;
                        }else {
                            layer.msg("最大长度为20");
                            return '';
                        }
                    }},
                    {field: 'content', title: '信息内容', fixed: 'left',minWidth: 850 , edit:'text', templet:function(d){
                        if(d.content.length <= 50){
                            return d.content;
                        }else {
                            layer.msg("最大长度为50");
                            return '';
                        }
                    }},
                    {fixed: 'right', title: '操作',width: 165, align:'center', toolbar: '#delMenu'}
                ]
            ],
            data: data
        });
    }

    // 新增
    $(".table_add").on('click',function () {
        let obj = {
            "title": '',
            "content": ''
        };
        cbList = table.cache.nonstandard;
        if(cbList.length === 10){
            layer.msg("最多添加10行");
            return false;
        }
        cbList.push(obj);
        renderDetailList(cbList);
        return false;
    });

    // 删除
    table.on('tool(nonstandard-table)', function(obj){
        var data = obj.data;//获得当前行数据
        let layEvent = obj.event;
        if(layEvent === 'del'){
            if (JSON.stringify(data) === "{}") {
              table.cache.nonstandard[table.cache.nonstandard.length - 1] = [];
            }
            obj.del();
            // 清空数组
            let nonstandard = table.cache.nonstandard;
            nonstandard = nonstandard.filter(function(item) {
                return item.length !== 0;
            });
            table.cache.nonstandard = nonstandard;
        }
    });


    function submit(data) {
        let nonstandard = table.cache.nonstandard;
        let breakFlag = false;
        nonstandard = nonstandard.filter(function(item) {
            if (item.title === '' || item.content === '') {
                layer.msg("请先填写信息名称和信息内容后再提交");
                breakFlag = true;
            }
            return item.length !== 0 && item.title !== '' && item.content !== '';
        });
        if (breakFlag) {
            return;
        }
        let url = "/work/modify/save/nonstandard.action";
        let imgSrc = getImgUrls().substr(1);
        data.field.imgStr = imgSrc;
        data.field.orderNonstandardList = nonstandard;
        $http.lockSave(url, data.field, data.elem).then(function () {
            layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                opCommonUtils.goOperateHome();
            });
        }, function (msg) {
            layer.msg(msg);
        });

    }

    function getImgUrls() {
        let imgUrls = "";
        let $img = $(".img-url");
        $img.each(function (ind, item) {
            let imgUrl = $(item).val();
            if (imgUrl) {
                imgUrls += "," + imgUrl;
            }
        });
        return imgUrls;
    }

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadImags;

    function uploaderImage(elemId, valueId) {
        upload.render({
            elem: '#' + elemId
            , exts: 'jpg|png|jpeg'
            , acceptMime: 'image/*'
            , accept: 'images'
            , url: '/work/modify/uploader/orderPhoto?timestamp=' + timestamp + '&elemId=' + elemId
            , size: 5120
            , beforeSend: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.statusBool) {
                    let data = res.data;
                    $("#" + valueId).val(data);
                    $("#" + valueId+"s").attr("src",data).attr("style", "width: 0;height: 0");
                    reLoadImage(elemId, data);
                } else {
                    layer.msg(res.message)
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }

    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
        let next = $("#" + elemId + "Value").next();
        let next1 = $(next).next();
        $(next).attr("style", "visibility:hidden !important");
        $(next1).attr("style", "visibility:hidden !important");
    }

    /**
     * 初始化页面
     */
    function initPage() {

        renderDetailList(orderNonstandardList);

        // 初始化文件上传插件
        for (let i = 0; i < 5; i++) {
            uploaderImage("imageSrcUrl" + i, "imageSrcUrl" + i + "Value");
        }
        // 回显已有图片
        let imageSrc = imageStr;
        if (imageSrc) {
            let imageSrcArr = imageSrc.split(",");
            imageSrcArr.forEach(function (url, index) {
                reLoadImage("imageSrcUrl" + index, url);
                $("#imageSrcUrl" + index + "Value").val(url);
                $("#imageSrcUrl" + index + "Values").attr("src",url).attr("style", "width: 0;height: 0");
            })
        }

    }

    /**
     * 初始化页面事件
     */
    function initEvent() {

        renderDetailList(orderNonstandardList);

        if(orderNonstandardList.length === 0){
            $(".table_add").trigger('click');
        }

        // 点击放大
        $(document).on('click', '.btn-review', function () {
            let $container = $(this).parent().prev();
            $container.viewer().on('hide', function () {
                $container.viewer('destroy');
            });

            let index = $(this).index();
            $container.viewer('view', index);

        });
        // 点击删除
        $(document).on('click', '.btn-delete', function () {
            let $uploadDiv = $(this).parent().prev();
            $uploadDiv.find(".layui-upload-drag").attr("style", "");
            $uploadDiv.find("i").attr("style", "");
            $uploadDiv.find("p").attr("style", "");
            $uploadDiv.find(":hidden").val("");
        });

        // 保存
        form.on('submit()', function (data) {
            submit(data);
            return false
        });

    }

    // 程序入口
    function init() {

        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();

    }

    // 初始化
    init();
    exports('oms_work_modify_nonstandard');
});
